from suds.client import Client
from config.redis_conf import r
from django.http import HttpResponse
from config.url_conf import hd_url
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

client = Client(url=hd_url)


def getEarlyResult(request):
    '''结清/划回回调'''
    # data = '{"account_number":"GD2021020201203590","status":"1","real_money":"40000.00","type":"2"}'
    contnum = request.GET.get('acn')
    data = r.hget('addSettleData_list', contnum)
    logging.info('恒易融异步回盘，结清/划回入参：{0}'.format(data))
    client.service.getEarlyResult(data)
    r.hdel('addSettleData_list', contnum)
    return HttpResponse(client.last_received())


def updateRepayStats(request):
    '''正常月还回调'''
    contractId = request.GET.get('contractId')
    data = r.hget('repayFund_list', contractId)
    logging.info('恒易融异步回盘，正常月还入参：{0}'.format(data))
    client.service.updateRepayStats(data)
    r.hdel('repayFund_list', contractId)
    logging.info('从redis中删除')
    return HttpResponse(client.last_received())


def getP2PResult(request):
    '''逾期还款回调'''
    contractId = request.GET.get('contractId')
    data = r.hget('repayOverdue_list', contractId)
    logging.info('恒易融异步回盘，逾期划扣入参：{0}'.format(data))
    client.service.getP2PResult(data)
    r.hdel('repayOverdue_list', contractId)
    return HttpResponse()
